<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>JSDoc: Source: request.js</title>

    <script src="scripts/prettify/prettify.js"> </script>
    <script src="scripts/prettify/lang-css.js"> </script>
    <!--[if lt IE 9]>
      <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
    <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

    <h1 class="page-title">Source: request.js</h1>

    



    
    <section>
        <article>
            <pre class="prettyprint source linenums"><code>
import { urlTransform, is, isNoDataMethod, isSameOrigin, isBrowser } from './utils';

/**
 * 构造请求体
 * @class Request
 */
class Request {
  /**
   * @constructor
   * @param {Header} header - 一个header对象
   * @param {Object} config - 配置对象
   */
  constructor(header, config) {
    this.header = header || {};
    this.method = config.method ? config.method.toLowerCase() : 'get';
    this.data = null;
    this.url = config.url || '';
    if (config.data) {
      this.data = config.data;
      // 当我们的方法是Get,Header,Options,Delete,Jsonp这样的方法时
      // 把data部分转为query string
      this.datafilter(config.baseUrl);
    }
    this.crossDomain = !isSameOrigin(this.url);
    this.inBrowser = isBrowser();

    this.clientOptions = {
      client: config.client,
      onProgress: config.onProgress,
      timeout: config.timeout || 0,
      withCredentials: config.withCredentials || false,
      responseType: config.responseType || '',
      xsrfCookieName: config.xsrfCookieName,
      xsrfHeaderName: config.xsrfHeaderName,
      maxContentLength: config.maxContentLength,
      successCheck: config.successCheck,
    };
  }

  datafilter(baseUrl) {
    if (isNoDataMethod(this.method)) {
      if (is('Object', this.data) || typeof this.data === 'string') {
        this.url = urlTransform(this.url, this.data, baseUrl || '');
        this.data = null;
      }
    }
  }

  setDataString(dataString) {
    this.data = dataString;
    this.datafilter();
  }
  setData(k, v) {
    if (this.data === null) {
      this.data = {};
      this.data[k] = v;
    } else if (!this.data[k]) {
      this.data[k] = v;
    }
    this.datafilter();
  }

  getHeader(name) {
    return this.header.get(name);
  }

  getClientOption(name) {
    if (!name) return '';
    return this.clientOptions[name];
  }

}

export default Request;
</code></pre>
        </article>
    </section>




</div>

<nav>
    <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-Core.html">Core</a></li><li><a href="module-JSONP.html">JSONP</a></li><li><a href="module-Utils.html">Utils</a></li><li><a href="module-XHR.html">XHR</a></li></ul><h3>Classes</h3><ul><li><a href="Header.html">Header</a></li><li><a href="InterceptorFactory.html">InterceptorFactory</a></li><li><a href="module-Core-Q.html">Q</a></li><li><a href="Request.html">Request</a></li><li><a href="Response.html">Response</a></li></ul><h3>Namespaces</h3><ul><li><a href="q.html">q</a></li></ul><h3>Global</h3><ul><li><a href="global.html#contentInterceptor">contentInterceptor</a></li><li><a href="global.html#defaultOptions">defaultOptions</a></li><li><a href="global.html#xrfsInterceptor">xrfsInterceptor</a></li></ul>
</nav>

<br class="clear">

<footer>
    Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.3</a> on Tue Apr 18 2017 23:31:24 GMT+0800 (CST)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
